package com.front.web.member;

import com.bootdo.common.utils.R;
import com.bootdo.member.domain.ShequMember;
import com.bootdo.member.domain.ShequUserAddress;
import com.front.web.commons.AvoidRepeatableCommit;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/f/shequ/SheQuMemberAddressController/")
public class SheQuMemberAddressController extends MemberSuperController{


    /**
     * 获取地址列表
     * @param mobile
     * @return
     */
    @RequestMapping("getMemberAddressListAll")
    public R getMemberAddressListAll(@RequestParam(value = "mobile",defaultValue = "")String mobile){
        ShequMember byMobile = shequMemberService.getByMobile(mobile);
        if(byMobile==null){
            return R.error("请重新登录");
        }
        HashMap<String, Object> param = new HashMap<>();
        param.put("uid",byMobile.getId());
        List<ShequUserAddress> list = shequUserAddressService.list(param);

        return R.ok().put("i",list);
    }


    /**
     * 获取默认地址信息(单个)
     * @param mobile
     * @return
     */
    @RequestMapping("getMemberAddressDefaults")
    public R getMemberAddressDefaults(@RequestParam(value = "mobile",defaultValue = "")String mobile){
        ShequMember byMobile = shequMemberService.getByMobile(mobile);
        if(byMobile==null){
            return R.error("请重新登录");
        }
        HashMap<String, Object> param = new HashMap<>();
        param.put("uid",byMobile.getId());
        param.put("isDefault",1);
        List<ShequUserAddress> list = shequUserAddressService.list(param);

        if(list.size()<=0||list==null){
            return  R.ok();
        }
        return R.ok().put("i",list.get(0));
    }

    /**
     * 编辑 修改,添加地址信息
     * @param shequUserAddress
     * @return
     */
    @AvoidRepeatableCommit
    @RequestMapping("updateOneMemberAddress")
    public R updateOneMemberAddress(ShequUserAddress shequUserAddress,@RequestParam(value = "mobile",defaultValue = "") String mobile){

        ShequMember byMobile = shequMemberService.getByMobile(mobile);
        if(byMobile==null){
            return R.error("请先登录再操作");
        }
        Integer isDefault = shequUserAddress.getIsDefault();
        if(isDefault==1){
            Long id = shequUserAddress.getId();
            if(id==null){
                id=0L;
            }
            shequUserAddressService.updateAllNotDefault(id,byMobile.getId());
        }
        shequUserAddress.setCommunityId(byMobile.getCommunityId());
        shequUserAddress.setCommunityName(byMobile.getCommunityName());
        shequUserAddress.setUid(byMobile.getId());
        shequUserAddress.setUserName(byMobile.getRealName());
        int save = shequUserAddressService.save(shequUserAddress);

        if(save>0){
            return R.ok();
        }

        return R.error("编辑失败");
    }

    /**
     * 获取单个地址信息
     * @param mobile        会员电话
     * @param addressId    单个地址id
     * @return
     */
    @RequestMapping("getMemberAddressOne")
    public R getMemberAddressOne(@RequestParam(value = "mobile",defaultValue = "")String mobile,
                                 @RequestParam(value = "addressId",defaultValue = "")Long addressId){
        HashMap<String, Object> param = new HashMap<>();
        param.put("receiverMobile",mobile);
        param.put("id",addressId);
        List<ShequUserAddress> list = shequUserAddressService.list(param);
        if(list.size()<=0||list==null){
            return R.error("地址信息有误，请刷新重试");
        }else{
            ShequUserAddress shequUserAddress = list.get(0);

            return R.ok().put("i",shequUserAddress);
        }


    }

    /**
     * 删除单个地址信息
     * @param mobile        会员电话
     * @param addressId    单个地址id
     * @return
     */
    @RequestMapping("delMemberAddressOne")
    public R delMemberAddressOne(@RequestParam(value = "mobile",defaultValue = "")String mobile,
                                 @RequestParam(value = "addressId",defaultValue = "")Long addressId){
        ShequMember byMobile = shequMemberService.getByMobile(mobile);
        HashMap<String, Object> param = new HashMap<>();
        param.put("uid",byMobile.getId());
        param.put("id",addressId);
        List<ShequUserAddress> list = shequUserAddressService.list(param);
        if(list.size()<=0||list==null){
            return R.error("地址信息有误，请刷新重试");
        }else{
            int remove = shequUserAddressService.remove(addressId);
            if(remove>0){

                return R.ok();
            }else {
                return R.error("删除失败");

            }
        }


    }
}
